package com.wh.springboot.controller;

import com.wh.springboot.common.Result;
import com.wh.springboot.common.entity.CovidData;
import com.wh.springboot.common.entity.LineTrend;
import com.wh.springboot.service.CovidService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 疫情图表
 */
@RestController
@RequestMapping("/covid")
public class CovidEchartsController {
    @Autowired
    private CovidService covidService;
    // 渲染为一个json数据
    // @ResponseBody

    /**
     * 中国地图
     * 查询所有数据
     *
     * @return
     */
    @GetMapping
    public Result queryDate() {
        //只需要倒叙的查看34个数据即可
        List<CovidData> list = covidService.listOrderbyLimit();
        return Result.success(list);
//        return Result.success(covidService.list());
    }

    /**
     * 饼图
     * 分组聚合
     * select count(*) from goods group by type;
     */

    @GetMapping("/querypie")
    public Result queryPieDate() {
        List<CovidData> list = covidService.listOrderbyLimit();

        return Result.success(list);
    }

    /**
     * 柱状图
     */
    @GetMapping("/querybar")
    public Result barPieDate() {

        //1.所有城市 数据:数值
//        List<CovidData> list = covidService.list();
        List<CovidData> list = covidService.listOrderbyLimit();

        //2.所有城市数据
        List<String> cityList = new ArrayList<>();
        for (CovidData covidData : list) {
            //城市的数据
            cityList.add(covidData.getName());
        }
        //3.所有疫情数值
        List<Integer> dataList = new ArrayList<>();
        for (CovidData covidData : list) {
            //城市的数据
            dataList.add(covidData.getValue());
        }
        //4. 创建Map
        Map<String, Object> map = new HashMap<>();
        map.put("cityList", cityList);
        map.put("dataList", dataList);
        return Result.success(map);

    }

    /**
     * 折线图
     * select * from qushishuju order by create_time limit 7
     */
    @GetMapping("/queryline")
    public Result querylineDate() {
        //1.近七天查询所有的数据
        List<LineTrend> listSeven = covidService.findSevenDate();
        //2.封装所有的确诊人数
        List<Integer> confirmedList = new ArrayList<>();
        //3.封装所有的隔离人数
        List<Integer> isolationList = new ArrayList<>();
        //4.封装所有的治愈人数
        List<Integer> cureList = new ArrayList<>();
        //5.封装所有的死亡人数
        List<Integer> deadList = new ArrayList<>();
        //6.封装所有的疑似人数
        List<Integer> similarList = new ArrayList<>();
        for (LineTrend lineTrend : listSeven) {
            confirmedList.add(lineTrend.getConfirm());
            isolationList.add(lineTrend.getIsolation());
            cureList.add(lineTrend.getCure());
            deadList.add(lineTrend.getDead());
            similarList.add(lineTrend.getSimilar());
        }
        //7. 返回数据的格式容器Map
        Map<String, Object> map = new HashMap();
        map.put("confirmedList", confirmedList);
        map.put("isolationList", isolationList);
        map.put("cureList", cureList);
        map.put("deadList", deadList);
        map.put("similarList", similarList);
        return Result.success(map);
    }


}
